package com.lovo.sgproj.service.imp;

import com.lovo.sgproj.bean.RecordBean;
import com.lovo.sgproj.bean.RoomBean;
import com.lovo.sgproj.bean.StudentBean;
import com.lovo.sgproj.dao.RecordDAO;
import com.lovo.sgproj.dao.RoomDAO;
import com.lovo.sgproj.dao.StudentDAO;
import com.lovo.sgproj.dao.imp.RecordDAOImp;
import com.lovo.sgproj.dao.imp.RoomDAOImp;
import com.lovo.sgproj.dao.imp.StudentDAOImp;
import com.lovo.sgproj.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
@Component
public class RoomServiceImp implements RoomService {
    @Autowired
    StudentDAO stuDAO;
    @Autowired
    RoomDAO roomDAO ;
    @Autowired
    RecordDAO recordDAO ;

    @Override
    public ArrayList<RoomBean> showAllRoom() {
        return roomDAO.findAllRoom();
    }

    @Override
    public ArrayList<RoomBean> showUserableRoom(boolean gender) {
        return roomDAO.findAllUserableRoom(gender);
    }

    @Override
    public ArrayList<RoomBean> showChangableRoom(StudentBean stu) {
        return roomDAO.findChangeableRoom(stu);
    }

    @Override
    public boolean addRoom(RoomBean room) {
        int repeat = roomDAO.getRoomNumByAddress(room.getAddress());
        if (repeat == 0) {
            roomDAO.insertRoom(room);
            return true;
        }
        return false;
    }

    @Override
    public boolean removeRoom(int roomId) {
        if (stuDAO.findStudentByRoomID(roomId).size() == 0) {
            roomDAO.deleteRoomByID(roomId);
            return true;
        }
        return false;
    }

    @Override
    public RoomBean showRoomByID(int roomId) {
        return roomDAO.getRoomAllDetailByID(roomId);
    }

    @Override
    public ArrayList<RecordBean> showRecordsByRoomID(int roomId) {
        return recordDAO.findAllRecordByRoomID(roomId);
    }

    @Override
    public ArrayList<RoomBean> showRoomByCondition(String address, int roomStatus, int availableLive, int roomType) {
        return roomDAO.findAviableRoom(address, roomStatus, availableLive, roomType);
    }
}
